//
//  MJStatueToolBar.swift
//  新浪微博
//
//  Created by MaJi on 16/4/13.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SnapKit
class MJStatueToolBar: UIView {

    //声明三个按钮  确保按钮一定会有值  不需要管我在哪个地方赋值

    var repostBtn:UIButton!
    var commentBtn:UIButton!
    var ohYeahBtn:UIButton!
    
//    从viewmodel中取数据
    var statusViewModel:MJststusOriganalVIewModel? {
        didSet{
            repostBtn.setTitle("\(statusViewModel?.status?.reposts_count ?? 0)", forState: .Normal)
                
            commentBtn.setTitle("\(statusViewModel?.status?.comments_count ?? 0)", forState: .Normal)
            
            ohYeahBtn.setTitle("\(statusViewModel?.status?.attitudes_count ?? 0)", forState: .Normal)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
    
        repostBtn = addChildrenButton("timeline_icon_retweet", title: "转发")
        commentBtn = addChildrenButton("timeline_icon_comment", title: "评论")
        ohYeahBtn = addChildrenButton("timeline_icon_like", title: "赞")
//        添加分割线
        addSubview(line2)
        addSubview(line1)
//        addSubview(line2)
//        设置自动布局的约束条件
        repostBtn.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self)
            make.height.equalTo(self)
            make.width.equalTo(commentBtn)
        }
    
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(repostBtn.snp_right)
            make.right.equalTo(ohYeahBtn.snp_left)
            make.top.equalTo(repostBtn.snp_top)
            make.width.equalTo(ohYeahBtn)
            make.height.equalTo(self)
        }
        ohYeahBtn.snp_makeConstraints { (make) -> Void in
            make.right.top.equalTo(self)
            make.height.equalTo(self)
        }
//        黄金分隔比
        let multiplied = 0.4
        line1.snp_makeConstraints { (make) -> Void in
            let scale = UIScreen.mainScreen().scale
            make.width.equalTo(1/scale)
            make.left.equalTo(repostBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
        }
        line2.snp_makeConstraints { (make) -> Void in
            let scale = UIScreen.mainScreen().scale
            make.width.equalTo(1/scale)
            make.left.equalTo(commentBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
        }
        
    }
    func addChildrenButton(imageName:String , title:String) -> UIButton{
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        btn.setTitle(title, forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(12)
//        btn.titleLabel?.textColor = UIColor.redColor()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.sizeToFit()
        addSubview(btn)
        return btn
    }
    
//    可以采用懒加载的方式也可以写成一个方法,返回一个UIView,这样只需要写一个方法就可一啦
    private lazy var line1:UIView = {
       let l = UIView()
        l.backgroundColor = UIColor.purpleColor()
        return l
    }()
    private lazy var line2:UIView = {
        let l = UIView()
        l.backgroundColor = UIColor.purpleColor()
        return l
    }()
}
